﻿
function btnLoginOver(){
	objBtnLogin.attr("src", "/image/login_focus.png");
}

function btnLoginOut(){
	objBtnLogin.attr("src", "/image/login_normal.png");
}

function btnLoginDown(){
	objBtnLogin.attr("src", "/image/login_ondown.png");
}

function btnLoginUp(){
	objBtnLogin.attr("src", "/image/login_focus.png");
}

//var syncvar;

function login_callback(jsondata){

	var cc = jsondata.clientcode;
	
	if (isNaN(cc))
		jsondata.clientcode = 0;
	
	objDivStateText.html(eval("LOGIN_STATE_" + jsondata.clientcode));

	if(jsondata.clientcode == 1){
		toMain(jsondata.myinfo, jsondata.groups);
		doInterface();	//登录成功，连接系统接口
	}else{
		backToLogin();
	}
}

//验证
function validatorLogin(){
	objDivStateText.html("");
	if(objTxtUsername.attr("value").length <1 || objTxtPassword.attr("value").length < 4)
		objDivStateText.html(LOCAL_STATE_1);
	else
		private_logining();
	objTxtUsername.focus();
}


function private_logining(){
	objDivStateText.html("");
	$("#loginInputDIV").css("display", "none");
	$("#isLoginingDIV").css("display", "block");
	$("#divOther").css("display", "none");

	objTxtUsername.attr("disabled", "disabled");
	objTxtPassword.attr("disabled", "disabled");
	objChkKeepPassword.attr("disabled", "disabled");
	objCboBgImage.attr("disabled", "disabled");
	
	doLogin();

}

function changeBG(index){
	public_setCookie("bgindex", index);
	public_changeBG(index);
}

//登录失败，返回登录界面
function backToLogin(){
	
	$("#isLoginingDIV").css("display", "none");
	$("#loginInputDIV").css("display", "block");
	
	$("#divOther").css("display", "block");

	objTxtUsername.removeAttr("disabled");
	objTxtPassword.removeAttr("disabled");
	objChkKeepPassword.removeAttr("disabled");
	objCboBgImage.removeAttr("disabled");

	objTxtUsername.focus();	

}



//登录成功才执行
function toMain(myinfo, groupsString){

	MYINFO = myinfo;
	//处理myInfo
	objImgMyFace.attr("src", "/image/userfaces/" + MYINFO.myid + ".jpg");

	//把转义符转换为HTML，否则转义符在input text里仍显示转义符，而不是转移后的HTML
	objTxtHeart.attr("value", transferToHTML(MYINFO.myheart));
	objTxtHeart.attr("title", transferToHTML(MYINFO.myheart));
	objDivMyNickname.html(MYINFO.mynickname);
	objDivMyNickname.attr("title", MYINFO.mynickname);


	//处理groupsString
	private_createGroups(groupsString);
	

	objTxtPassword.attr("value", "");	//清空密码输入框
	$("#divOther").css("display", "block");
	$("#isLoginingDIV").css("display", "none");
	
	
	objCboBgImage.removeAttr("disabled");
	objCboBgImage.css("margin-left", "10px");
	objCboBgImage.appendTo(objToolBar);
	
	objLoginInputDIV.css("display", "none");
	objDivMyInfo.css("display", "block");
	objFriendDIV.css("display", "block");
	objLoginDIV.css("margin-left", screen_width - 240 - 60 + "px");

	objStatebar.css("display", "block");
	objToolBar.css("display", "block");
	
	
	//禁止选定
//	if(browser_type.indexOf("ie") >= 0)
//		document.body.onselectstart = function(){return false;};
//	else if(browser_type == "firefox"){
//			$("*").css("-moz-user-select", "-moz-none"); // 禁止选中文字
//	}
	
	$.messager.show('系统消息', '添加好友成功！', 0);
	//$("#chatWindow").css("display", "block");
}

function private_createGroups(groupsString){
	
	//遍历每组
	$(groupsString).each(function(i, v){

		myfriendgroups[++myfriendgroups_index] = new Array();
		

		var liGroupItem = document.createElement("li");
		
		var aGroupName = document.createElement("a");
		//aGroupName.innerHTML = this.groupname + " [0/5]";
		aGroupName.className = "collapsed";
		liGroupItem.appendChild(aGroupName);	//添加aGroupName
		
		var ulFriends = document.createElement("ul");
		
		//第一组不隐藏
		if(i > 0)
			ulFriends.style.display = "none";
		
		var groupFriendsCount = 0;
		var olCount = 0;
		
		if(this.friendString.length > 1){
			//遍历数组，获得某组的所有好友

			$.each(this.friendString.split(' '), function(index, value) {

				//123'nick$hmemo<
				
				//在线<
				//离线>
				
				var heart = "";
				var heartIndex = value.indexOf("<");
				if(heartIndex < 0)
					heartIndex = value.indexOf(">");
				heart = value.substring(heartIndex+1);
				var isOnline = (value.indexOf("<") > 0) ? true:false;
				
				if(isOnline)
					olCount++;
				value = value.substring(0, heartIndex);
				
				var uidIndex = value.indexOf("'");
				var uid = value.substring(0, uidIndex);
				
				var memo = "";

				var memoIndex = value.indexOf("$");
				if(memoIndex >= 0)
					memo = value.substring(memoIndex+1);

			
				var nickname = memoIndex >=0 ? value.substring(uidIndex+1, memoIndex):value.substring(uidIndex+1);

				//添加好友Item到数组
				myfriendgroups[myfriendgroups_index][index] = {
						"userid":uid, 
						"nickname":nickname, 
						"memo":memo, 
						"heart":heart, 
						"isOnline":isOnline};
				
				if(isOnline){
				var liFriendItem = document.createElement("li");

				$(liFriendItem).attr("id", "lifrienditem_" + uid);
				
				if(memo == ""){
					$(liFriendItem).attr("title", nickname + "，在线");
				}else{
					$(liFriendItem).attr("title", memo + "(" + nickname + ")，在线");
				}
				//$(liFriendItem).attr("title", nickname + (memo == "" ? "":"(" + memo + ")") + "，在线");
				$(liFriendItem).addClass("frienditem");
				

				var imgFace = document.createElement("img");
				//alert("在线___"+uid);
				$(imgFace).attr("src", "/image/userfaces/" + uid + ".jpg");

				$(imgFace).addClass("friendlistface");

				var spanNickname = document.createElement("span");
				
				if(memo == ""){
					$(spanNickname).html(nickname);
				}else{
					$(spanNickname).html(memo + "(" + nickname + ")");
				}
				
				//$(spanNickname).html(nickname+(memo == "" ? "":"(" + memo + ")"));

				$(liFriendItem).append($(imgFace));
				$(liFriendItem).append($(spanNickname));


				$(ulFriends).append($(liFriendItem));
				}
				
				
				groupFriendsCount = index+1;
	        });  
		}	//组内循环结束
		
		//显示不在线
		$.each(myfriendgroups[myfriendgroups_index], function(index, value) {
			var tu = myfriendgroups[myfriendgroups_index][index];

			
			if(!tu.isOnline){
				var uid = tu.userid;
				var nickname = tu.nickname;
				var memo = tu.memo;

				var liFriendItem = document.createElement("li");

				
				$(liFriendItem).attr("id", "lifrienditem_" + uid);
				if(memo == ""){
					$(liFriendItem).attr("title", nickname + "，离线");
				}else{
					$(liFriendItem).attr("title", memo + "(" + nickname + ")，离线");
				}
				
				//$(liFriendItem).attr("title", nickname + (memo == "" ? "":"(" + memo + ")") + "，离线");
				$(liFriendItem).addClass("frienditem");
				

				var imgFace = document.createElement("img");

				$(imgFace).attr("src", "/image/userfaces/" + uid + ".jpg");

				$(imgFace).addClass("friendlistface friendlistfaceoutline");

				var spanNickname = document.createElement("span");
				
				if(memo == ""){
					$(spanNickname).html(nickname);
				}else{
					$(spanNickname).html( memo + "(" + nickname + ")");
				}
				//$(spanNickname).html(nickname+(memo == "" ? "":"(" + memo + ")"));

				$(liFriendItem).append($(imgFace));
				$(liFriendItem).append($(spanNickname));


				$(ulFriends).append($(liFriendItem));
			}
			
		});
		
		

		$(aGroupName).html(this.groupname + " [" + olCount + "/" + (groupFriendsCount) + "]");

		$(liGroupItem).append($(ulFriends));
		
		$("#zd").append(liGroupItem);

		//alert(this.groupname+"----------"+this.friendString);

	});		//一组结束
	

	//好友分组菜单功能
	$('#zd > li > a').click(
			function() {
				$(this).next('ul').get(0).style.display == 'none' ? $(this).removeClass('collapsed') : $(this).addClass('collapsed');
				$(this).next('ul').slideToggle(200);
	});
	


}

function txtInputOver(obj){
	obj.css("border-color", "#088A08");
}

function txtInputOut(obj){
	obj.css("border-color", "");
}

var currentHeart = null;
function focusTxtHeart(){
	
	currentHeart = objTxtHeart.attr("value");
	objTxtHeart.css("border", "1px solid #0000FF");
}

function blurTxtHeart(){
	objTxtHeart.attr("value", currentHeart);
	objTxtHeart.css("border", "0px");
}

function enterTxtHeart(){
	//AJAX提交心情
	currentHeart = objTxtHeart.attr("value");
	objTxtHeart.attr("title", currentHeart);
	objTxtHeart.blur();
}

function sendChatContent(event){
	var userid = public_getIdFromString(event.target.id);
	var o = $("#enterChatContent_" + userid);
	if(o.html() == ""){
		o.focus();
		return;
	}

	var chatContentMyNickname = document.createElement("div");
	$(chatContentMyNickname).addClass("chatContentMyNickname");
	$(chatContentMyNickname).html(MYINFO.mynickname + " " + public_getCurrentDateTime());
	var chatContentMyContente = document.createElement("div");
	$(chatContentMyContente).addClass("chatContent");
	$(chatContentMyContente).html(o.html());
	
	var con = $("#chatContentShow_" + userid);
	con.append(chatContentMyNickname);
	//con.append("<br />");
	con.append(chatContentMyContente);
	//con.append("<br /><br />");
	con.animate({scrollTop: con[0].scrollHeight});
	doSendChat(userid, $(o).html());
	o.empty();
	o.focus();

}

function clearContent(event){
	var userid = public_getIdFromString(event.target.id);
	$("#chatContentShow_" + userid).empty();
	$("#enterChatContent_" + userid).focus();
}